package com.shgx.server;

import com.shgx.grpc.api.RPCDateLszRequest;
import com.shgx.grpc.api.RPCDateLszResponse;
import com.shgx.grpc.api.RPCDateLszServiceGrpc;
import io.grpc.stub.StreamObserver;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author: guangxush
 * @create: 2019/07/07
 */
public class RPCDateLszServiceImpl extends RPCDateLszServiceGrpc.RPCDateLszServiceImplBase {
    @Override
    public void getDateLsz(RPCDateLszRequest request, StreamObserver<RPCDateLszResponse> responseObserver) {
        RPCDateLszResponse rpcDateResponse = null;
        Date now = new Date();
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("lsz今天是" + "yyyy年MM月dd日 E kk点mm分");
            String nowTime = simpleDateFormat.format(now);
            rpcDateResponse = RPCDateLszResponse
                    .newBuilder()
                    .setServerDate("Welcome " + request.getUserName() + ", " + nowTime)
                    .build();
        } catch (Exception e) {
            responseObserver.onError(e);
        } finally {
            responseObserver.onNext(rpcDateResponse);
        }
        responseObserver.onCompleted();
    }
}